home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14899 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  133 lines

  1. Path: news.gate.net!news-adm
  2. From: Howard Gardner <hgardner@gate.net>
  3. Newsgroups: comp.lang.c++
  4. Subject: Pointer Comparison
  5. Date: Tue, 02 Apr 1996 11:37:57 -0500
  6. Organization: CyberGate, Inc.
  7. Message-ID: <316157E5.4180@gate.net>
  8. NNTP-Posting-Host: jaxfl2-22.gate.net
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  13.  
  14. Consider this function:
  15.  
  16. int isLess
  17. (
  18.   const char * left,
  19.   const char * right
  20. )
  21. {
  22.  
  23.   return ( left < right ); // Compare POINTERS, not values
  24.  
  25. }
  26.  
  27. I believe that the result of this function is officialy undefined. I am 
  28. trying
  29. to determine whether depending on it is merely unsafe, or is downright
  30. foolhardy. It seems a statistical approach is about the only way to 
  31. determine
  32. this. Here are the assertions I need to test:
  33.  
  34.   1) No compiler exists for which this function will not
  35.      compile.
  36.   2) No compiler exists for which isLess( a, b ) and
  37.      isLess( b, a ) will both return non-zero.
  38.   3) No compiler exists for which isLess( a, a ) will
  39.      return non-zero.
  40.  
  41. For a compiler on which these assumptions are correct, the
  42. program below will print:
  43.  
  44. OK
  45. OK
  46.  
  47. If the assumptions are not correct, the program may not compile, or
  48. it will print something else.
  49.  
  50. I would appreciate it if everyone tried it with their compiler, and 
  51. reported
  52. the results via e-mail or comp.lang.c++.moderated. Please mention the
  53. compiler's name and version.
  54.  
  55. // Cut here
  56. #include <iostream.h>
  57.  
  58. int isLess
  59. (
  60.   const char * left,
  61.   const char * right
  62. )
  63. {
  64.  
  65.   return ( left < right ); // Compare POINTERS, not values
  66.  
  67. }
  68.  
  69. int main
  70. (
  71.   int,
  72.   char **
  73. )
  74. {
  75.  
  76.   char lAVal;
  77.   char lBVal;
  78.  
  79.   char * lAPtr = &lAVal;
  80.   char * lBPtr = &lBVal;
  81.  
  82.   if ( isLess ( lAPtr, lBPtr ) )
  83.   {
  84.  
  85.     if ( isLess ( lBPtr, lAPtr ) )
  86.     {
  87.  
  88.       cout << "ERROR" << endl;
  89.  
  90.     }
  91.     else
  92.     {
  93.  
  94.       cout << "OK" << endl;
  95.  
  96.     }
  97.  
  98.   }
  99.  
  100.   else if ( isLess ( lBPtr, lAPtr ) )
  101.   {
  102.  
  103.     if ( isLess ( lAPtr, lBPtr ) )
  104.     {
  105.  
  106.       cout << "ERROR" << endl;
  107.  
  108.     }
  109.  
  110.     else
  111.     {
  112.  
  113.       cout << "OK" << endl;
  114.  
  115.     }
  116.  
  117.   }
  118.  
  119.   if ( isLess( lAPtr, lAPtr ) )
  120.   {
  121.  
  122.     cout << "ERROR" << endl;
  123.  
  124.   }
  125.   else
  126.   {
  127.  
  128.     cout << "OK" << endl;
  129.  
  130.   }
  131.  
  132. }
  133.